
package org.ceteca.explica.server.common.bl;

import java.util.ArrayList;

import org.ceteca.explica.core.ExplicaException;
import org.ceteca.explica.core.common.BarcodeTypeOT;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.VatTypeOT;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.server.ServerContext;
import org.ceteca.explica.server.common.dao.BarcodeTypeDAO;
import org.ceteca.explica.server.common.dao.CommonDAO;
import org.ceteca.explica.server.common.dao.VatTypeDAO;
import org.ceteca.explica.server.util.db.CriteriaBean;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class CommonBLL {
	/** Unique instance of the ManagementBLL class. Singleton pattern */
	private static CommonBLL instance;

	/** Returns the unique instance of the ManagementBLL class.
	 * Implements the singleton dessign pattern.
	 * @return ManagementBLL, unique instance of ManagementBLL.
	 */
	public static final CommonBLL getInstance() {
		if (instance == null)
			instance = new CommonBLL();
		return instance;
	}

	/**
	 * Default constructor.
	 */
	private CommonBLL() {

	}

	public ArrayList queryBarcodeTypes(ArrayList <CriteriaBean> lCriteria) throws ExplicaException {
		// Call the DAO to query the database and get the barcode types.
		BarcodeTypeDAO dao = new BarcodeTypeDAO();
		ArrayList lBarcodeTypes = dao.getBarcodeTypes(lCriteria);
		
		ServerContext.getInstance().getLogger().info("List of barcode types: " + lBarcodeTypes);
		
		return lBarcodeTypes;
	}
	
	public OperationResult delBarcodeType(Long id) throws ExplicaException {
		// Call the DAO to remove the barcode type from the database.
		BarcodeTypeDAO dao = new BarcodeTypeDAO();
		return dao.delBarcodeType(id);
	}
	
	public OperationResult delBarcodeTypes(ArrayList<Long> lSelBarcodeTypes) throws ExplicaException {
		// Call the DAO to remove the barcode types from the database.
		BarcodeTypeDAO dao = new BarcodeTypeDAO();
		return dao.delBarcodeTypes(lSelBarcodeTypes);
	}
	
	public OperationResult saveBarcodeType(BarcodeTypeOT barcodeType) throws ExplicaException {
		// Call the DAO to save the barcode type in the database.
		BarcodeTypeDAO dao = new BarcodeTypeDAO();
		return dao.saveBarcodeType(barcodeType);
	}
	
	public BarcodeTypeOT getBarcodeType(Long id) throws ExplicaException {
		BarcodeTypeDAO dao = new BarcodeTypeDAO();
		return dao.getBarcodeType(id.longValue());
	}
	
	public ArrayList queryVatTypes(ArrayList <CriteriaBean> lCriteria) throws ExplicaException {
		// Call the DAO to query the database and get the barcode types.
		VatTypeDAO dao = new VatTypeDAO();
		ArrayList lVatTypes = dao.getVatTypes(lCriteria);
		
		ServerContext.getInstance().getLogger().info("List of VAT types: " + lVatTypes);
		
		return lVatTypes;
	}
	
	public OperationResult delVatType(Long id) throws ExplicaException {
		// Call the DAO to remove the barcode type from the database.
		VatTypeDAO dao = new VatTypeDAO();
		return dao.delVatType(id);
	}
	
	public OperationResult delVatTypes(ArrayList<Long> lSelVatTypes) throws ExplicaException {
		// Call the DAO to remove the barcode types from the database.
		VatTypeDAO dao = new VatTypeDAO();
		return dao.delVatTypes(lSelVatTypes);
	}
	
	public OperationResult saveVatType(VatTypeOT vatType) throws ExplicaException {
		// Call the DAO to save the VAT type in the database.
		VatTypeDAO dao = new VatTypeDAO();
		return dao.saveVatType(vatType);
	}
	
	public VatTypeOT getVatType(Long id) throws ExplicaException {
		VatTypeDAO dao = new VatTypeDAO();
		return dao.getVatType(id.longValue());
	}
	

	

	public BaseVO getEntity(Long id, Class myClass) throws ExplicaException{
		CommonDAO myDAO=new CommonDAO();
		return myDAO.getEntity(id, myClass);	
		
	}

	
	public ArrayList<BaseVO> queryEntities(ArrayList <CriteriaBean> lCriteria, Class myClass) throws ExplicaException {

		CommonDAO dao = new CommonDAO();
		ArrayList<BaseVO> lEntities = dao.getEntities(lCriteria, myClass);
		return lEntities;
	}
	

	public ArrayList<BaseVO> queryEntities(String hql) throws ExplicaException{
		CommonDAO dao = new CommonDAO();
		ArrayList<BaseVO> lEntities = dao.getEntities(hql);
		return lEntities;
	}
	

		public OperationResult delEntities(ArrayList<Long> lSelected, Class myClass) throws ExplicaException {

			CommonDAO dao = new CommonDAO();
			return dao.delEntities(lSelected, myClass);
		}
		
		public OperationResult saveEntity(BaseVO entity) throws ExplicaException {
			CommonDAO dao = new CommonDAO();
			return dao.saveEntity(entity);
		}
		
	


	public OperationResult delEntity(BaseVO b) throws ExplicaException{

		CommonDAO dao = new CommonDAO();
		return dao.delEntity(b);
	}
}